#pragma once

#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <unistd.h>
#include <functional>

using func = std::function<void ()>;  // 包装器, 把函数包装起来, 可以放入vector中
std::vector<func> callback;  // 用来存储任务
std::unordered_map<int, std::string> desc;  // 用来查看任务列表

void readMysql()
{
    std::cout << "sub process " << getpid() << ": " << "执行访问数据库的任务" << std::endl;
}

void exculeUrl()
{
    std::cout << "sub process" << getpid() << ": " << "执行Url解析" << std::endl;
}

void cal()
{
    std::cout << "sub process" << getpid() << ": " << "执行加密任务" << std::endl;
}

void save()
{
    std::cout << "sub process" << getpid() << ": " << "执行数据持久化任务" << std::endl;
}

void load()  // 把任务放入vector和map中
{
    desc.insert({callback.size(), "readMysql: 读取数据库"}); // 本来需要使用make_pair
    callback.push_back(readMysql);
    desc.insert(std::make_pair(callback.size(), "execuleUrl: 进行Url解析"));
    callback.push_back(exculeUrl);
    desc.insert({callback.size(), "cal: 进行加密计算"});
    callback.push_back(cal);
    desc.insert(std::make_pair(callback.size(), "save: 进行持久化保存"));
    callback.push_back(save);
}

void Showhandler() // 打印任务列表
{
    for (auto& iter : desc)
    {
        std::cout << iter.first << "\t" << iter.second << std::endl;
    }
}

int HandlerSize()
{
    return callback.size();
}
